<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单发价格" prop="unitPrice">
        <el-input v-model="queryParams.unitPrice" placeholder="请输入单发价格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总奖池" prop="totalPrice">
        <el-input v-model="queryParams.totalPrice" placeholder="请输入总奖池" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="利润" prop="profit">
        <el-input v-model="queryParams.profit" placeholder="请输入利润" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否公开显示" prop="ifPublicShow">
        <el-input v-model="queryParams.ifPublicShow" placeholder="请输入是否公开显示" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="0下架 1上架" prop="boxStatus">
        <el-input v-model="queryParams.boxStatus" placeholder="0下架 1上架" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="关联无限赏盒子ID" prop="wxboxid">
        <el-input v-model="queryParams.wxboxid" placeholder="请输入关联无限赏盒子ID" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:vs:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:vs:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:vs:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:vs:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="箱子ID" align="center" prop="boxId" />
      <el-table-column label="单发价格" align="center" prop="unitPrice" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="图片地址" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="总奖池" align="center" prop="totalPrice" />
      <el-table-column label="利润" align="center" prop="profit" />
      <el-table-column label="充值可见" align="center" prop="ifPublicShow" />
      <el-table-column label="抽奖概率" align="center" prop="draw" />
      <el-table-column sortable="custom" label="状态" align="center" prop="boxStatus">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.boxStatus == 1">上架</el-tag>
          <el-tag type="danger" v-if="scope.row.boxStatus == 0">下架</el-tag>
          
        </template>
      </el-table-column>
      <el-table-column label="关联无限赏盒子ID" align="center" prop="wxboxid" />
      <el-table-column label="0碰概率" align="center" prop="pro0" />
      <el-table-column label="1碰概率" align="center" prop="pro1" />
      <el-table-column label="2碰概率" align="center" prop="pro2" />
      <el-table-column label="3碰概率" align="center" prop="pro3" />
      <el-table-column label="4碰概率" align="center" prop="pro4" />
      <el-table-column label="5碰概率" align="center" prop="pro5" />
      <el-table-column label="6碰概率" align="center" prop="pro6" />
      <el-table-column label="7碰概率" align="center" prop="pro7" />
      <el-table-column label="8碰概率" align="center" prop="pro8" />
      <el-table-column label="9碰概率" align="center" prop="pro9" />
      <el-table-column label="10碰概率" align="center" prop="pro10" />
      <el-table-column label="0碰商品ID" align="center" prop="goodsListId0" />
      <el-table-column label="1碰赏品ID" align="center" prop="goodsListId1" />
      <el-table-column label="2碰赏品ID" align="center" prop="goodsListId2" />
      <el-table-column label="3碰赏品ID" align="center" prop="goodsListId3" />
      <el-table-column label="4碰赏品ID" align="center" prop="goodsListId4" />
      <el-table-column label="5碰赏品ID" align="center" prop="goodsListId5" />
      <el-table-column label="6碰赏品ID" align="center" prop="goodsListId6" />
      <el-table-column label="7碰赏品ID" align="center" prop="goodsListId7" />
      <el-table-column label="8碰赏品ID" align="center" prop="goodsListId8" />
      <el-table-column label="9碰赏品ID" align="center" prop="goodsListId9" />
      <el-table-column label="10碰赏品ID" align="center" prop="goodsListId10" />
      <el-table-column label="参与对碰赏品1" align="center" prop="vsGoodsId1" />
      <el-table-column label="参与对碰赏品2" align="center" prop="vsGoodsId2" />
      <el-table-column label="参与对碰赏品3" align="center" prop="vsGoodsId3" />
      <el-table-column label="参与对碰赏品4" align="center" prop="vsGoodsId4" />
      <el-table-column label="参与对碰赏品5" align="center" prop="vsGoodsId5" />
      <el-table-column label="参与对碰赏品6" align="center" prop="vsGoodsId6" />
      <el-table-column label="参与对碰赏品7" align="center" prop="vsGoodsId7" />
      <el-table-column label="参与对碰赏品8" align="center" prop="vsGoodsId8" />
      <el-table-column label="参与对碰赏品9" align="center" prop="vsGoodsId9" />
      <el-table-column label="参与对碰赏品10" align="center" prop="vsGoodsId10" />
      <el-table-column label="参与对碰赏品11" align="center" prop="vsGoodsId11" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vs:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:vs:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改VS对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单发价格" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单发价格" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片地址">
          <image-upload v-model="form.imgUrl" />
        </el-form-item>
        <el-form-item label="上下架" prop="ifLs">
          <el-switch v-model.number="form.boxStatus" active-color="#13ce66" inactive-color="#F4F4F5" :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="0碰商品ID" prop="goodsListId0">
          <el-input v-model="form.goodsListId0" placeholder="请输入0碰商品ID" @focus="importExcel('goodsListId0')" />
        </el-form-item>
        <el-form-item label="1碰赏品ID" prop="goodsListId1">
          <el-input v-model="form.goodsListId1" placeholder="请输入1碰赏品ID" @focus="importExcel('goodsListId1')" />
        </el-form-item>
        <el-form-item label="2碰赏品ID" prop="goodsListId2">
          <el-input v-model="form.goodsListId2" placeholder="请输入2碰赏品ID" @focus="importExcel('goodsListId2')" />
        </el-form-item>
        <el-form-item label="3碰赏品ID" prop="goodsListId3">
          <el-input v-model="form.goodsListId3" placeholder="请输入3碰赏品ID" @focus="importExcel('goodsListId3')" />
        </el-form-item>
        <el-form-item label="4碰赏品ID" prop="goodsListId4">
          <el-input v-model="form.goodsListId4" placeholder="请输入4碰赏品ID" @focus="importExcel('goodsListId4')" />
        </el-form-item>
        <el-form-item label="5碰赏品ID" prop="goodsListId5">
          <el-input v-model="form.goodsListId5" placeholder="请输入5碰赏品ID" @focus="importExcel('goodsListId5')" />
        </el-form-item>
        <el-form-item label="6碰赏品ID" prop="goodsListId6">
          <el-input v-model="form.goodsListId6" placeholder="请输入6碰赏品ID" @focus="importExcel('goodsListId6')" />
        </el-form-item>
        <el-form-item label="7碰赏品ID" prop="goodsListId7">
          <el-input v-model="form.goodsListId7" placeholder="请输入7碰赏品ID" @focus="importExcel('goodsListId7')" />
        </el-form-item>
        <el-form-item label="8碰赏品ID" prop="goodsListId8">
          <el-input v-model="form.goodsListId8" placeholder="请输入8碰赏品ID" @focus="importExcel('goodsListId8')" />
        </el-form-item>
        <el-form-item label="9碰赏品ID" prop="goodsListId9">
          <el-input v-model="form.goodsListId9" placeholder="请输入9碰赏品ID" @focus="importExcel('goodsListId9')" />
        </el-form-item>
        <el-form-item label="10碰赏品ID" prop="goodsListId10">
          <el-input v-model="form.goodsListId10" placeholder="请输入10碰赏品ID" @focus="importExcel('goodsListId10')" />
        </el-form-item>
        <el-form-item label="0碰概率" prop="pro0">
          <el-input v-model="form.pro0" placeholder="请输入0碰概率" />
        </el-form-item>
        <el-form-item label="1碰概率" prop="pro1">
          <el-input v-model="form.pro1" placeholder="请输入1碰概率" />
        </el-form-item>
        <el-form-item label="2碰概率" prop="pro2">
          <el-input v-model="form.pro2" placeholder="请输入2碰概率" />
        </el-form-item>
        <el-form-item label="3碰概率" prop="pro3">
          <el-input v-model="form.pro3" placeholder="请输入3碰概率" />
        </el-form-item>
        <el-form-item label="4碰概率" prop="pro4">
          <el-input v-model="form.pro4" placeholder="请输入4碰概率" />
        </el-form-item>
        <el-form-item label="5碰概率" prop="pro5">
          <el-input v-model="form.pro5" placeholder="请输入5碰概率" />
        </el-form-item>
        <el-form-item label="6碰概率" prop="pro6">
          <el-input v-model="form.pro6" placeholder="请输入6碰概率" />
        </el-form-item>
        <el-form-item label="7碰概率" prop="pro7">
          <el-input v-model="form.pro7" placeholder="请输入7碰概率" />
        </el-form-item>
        <el-form-item label="8碰概率" prop="pro8">
          <el-input v-model="form.pro8" placeholder="请输入8碰概率" />
        </el-form-item>
        <el-form-item label="9碰概率" prop="pro9">
          <el-input v-model="form.pro9" placeholder="请输入9碰概率" />
        </el-form-item>
        <el-form-item label="10碰概率" prop="pro10">
          <el-input v-model="form.pro10" placeholder="请输入10碰概率" />
        </el-form-item>
        <el-form-item label="参与对碰赏品1" prop="vsGoodsId1">
          <el-input v-model="form.vsGoodsId1" placeholder="请输入参与对碰赏品1" @focus="importExcel('vsGoodsId1')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品2" prop="vsGoodsId2">
          <el-input v-model="form.vsGoodsId2" placeholder="请输入参与对碰赏品2" @focus="importExcel('vsGoodsId2')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品3" prop="vsGoodsId3">
          <el-input v-model="form.vsGoodsId3" placeholder="请输入参与对碰赏品3" @focus="importExcel('vsGoodsId3')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品4" prop="vsGoodsId4">
          <el-input v-model="form.vsGoodsId4" placeholder="请输入参与对碰赏品4" @focus="importExcel('vsGoodsId4')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品5" prop="vsGoodsId5">
          <el-input v-model="form.vsGoodsId5" placeholder="请输入参与对碰赏品5" @focus="importExcel('vsGoodsId5')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品6" prop="vsGoodsId6">
          <el-input v-model="form.vsGoodsId6" placeholder="请输入参与对碰赏品6" @focus="importExcel('vsGoodsId6')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品7" prop="vsGoodsId7">
          <el-input v-model="form.vsGoodsId7" placeholder="请输入参与对碰赏品7" @focus="importExcel('vsGoodsId7')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品8" prop="vsGoodsId8">
          <el-input v-model="form.vsGoodsId8" placeholder="请输入参与对碰赏品8" @focus="importExcel('vsGoodsId8')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品9" prop="vsGoodsId9">
          <el-input v-model="form.vsGoodsId9" placeholder="请输入参与对碰赏品9" @focus="importExcel('vsGoodsId9')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品10" prop="vsGoodsId10">
          <el-input v-model="form.vsGoodsId10" placeholder="请输入参与对碰赏品10" @focus="importExcel('vsGoodsId10')" />
        </el-form-item>
        <el-form-item label="参与对碰赏品11" prop="vsGoodsId11">
          <el-input v-model="form.vsGoodsId11" placeholder="请输入参与对碰赏品11" @focus="importExcel('vsGoodsId11')" />
        </el-form-item>
        <el-form-item label="总奖池" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总奖池" />
        </el-form-item>
        <el-form-item label="利润" prop="profit">
          <el-input v-model="form.profit" placeholder="请输入利润" />
        </el-form-item>
        <el-form-item label="充值可见" prop="ifPublicShow">
          <el-input v-model="form.ifPublicShow" placeholder="请输入充值可见" />
        </el-form-item>
        <el-form-item label="抽奖概率" prop="draw">
          <el-input v-model="form.draw" placeholder="请输入抽奖概率" />
        </el-form-item>
        <el-form-item label="关联无限赏盒子ID" prop="wxboxid">
          <el-input v-model="form.wxboxid" placeholder="请输入关联无限赏盒子ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 快捷导入盒子商品 -->
    <el-dialog title="快捷导入" :visible.sync="dialogVisible" width="80%" center>
      <importForm :box_id="box_id" @func="update" />
    </el-dialog>
  </div>
</template>

<script>
import { listVs, getVs, delVs, addVs, updateVs } from "@/api/system/vs";
import importForm from "@/components/importForm";

export default {
  components: {
    importForm
  },
  name: "Vs",
  data() {
    return {
      box_id: null,
      form_key: null,
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // VS表格数据
      vsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitPrice: null,
        title: null,
        description: null,
        imgUrl: null,
        goodsListId0: null,
        pro0: null,
        pro1: null,
        pro2: null,
        pro3: null,
        pro4: null,
        pro5: null,
        pro6: null,
        pro7: null,
        pro8: null,
        pro9: null,
        pro10: null,
        goodsListId1: null,
        goodsListId2: null,
        goodsListId3: null,
        goodsListId4: null,
        goodsListId5: null,
        goodsListId6: null,
        goodsListId7: null,
        goodsListId8: null,
        goodsListId9: null,
        goodsListId10: null,
        vsGoodsId1: null,
        vsGoodsId2: null,
        vsGoodsId3: null,
        vsGoodsId4: null,
        vsGoodsId5: null,
        vsGoodsId6: null,
        vsGoodsId7: null,
        vsGoodsId8: null,
        vsGoodsId9: null,
        vsGoodsId10: null,
        vsGoodsId11: null,
        totalPrice: null,
        profit: null,
        ifPublicShow: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    importExcel(val) {
      this.form_key = val;
      this.box_id = this.form.wxboxid;
      console.log(this.form_key);

      this.dialogVisible = true;
    },

    //接收一键导入表单数据
    update(value) {
      console.log(value);
      this.dialogVisible = false;

      //判断this.form_key是否包含goodsListId
      if (this.form_key.indexOf('goodsListId') != -1) {
        //获取goodsListId的数字
        let num = parseInt(this.form_key.replace(/[^0-9]/ig, ""));

        this.form['vsGoodsId' + (num + 1)] = value.id;
        console.log('dffdfdfd' + this.form['vsGoodsId' + num + 1]);

        this.form[this.form_key] = value.id;
      } else {
        this.form[this.form_key] = value.id;
      }


      console.log('dddd');
    },
    /** 查询VS列表 */
    getList() {
      this.loading = true;
      listVs(this.queryParams).then(response => {
        this.vsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        boxId: null,
        unitPrice: null,
        title: null,
        description: null,
        imgUrl: null,
        goodsListId0: null,
        pro0: null,
        pro1: null,
        pro2: null,
        pro3: null,
        pro4: null,
        pro5: null,
        pro6: null,
        pro7: null,
        pro8: null,
        pro9: null,
        pro10: null,
        goodsListId1: null,
        goodsListId2: null,
        goodsListId3: null,
        goodsListId4: null,
        goodsListId5: null,
        goodsListId6: null,
        goodsListId7: null,
        goodsListId8: null,
        goodsListId9: null,
        goodsListId10: null,
        vsGoodsId1: null,
        vsGoodsId2: null,
        vsGoodsId3: null,
        vsGoodsId4: null,
        vsGoodsId5: null,
        vsGoodsId6: null,
        vsGoodsId7: null,
        vsGoodsId8: null,
        vsGoodsId9: null,
        vsGoodsId10: null,
        vsGoodsId11: null,
        totalPrice: null,
        profit: null,
        ifPublicShow: null,
        createTime: null,
        updateTime: null,
        draw: null,
        boxStatus: 0,
        wxboxid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.boxId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加VS";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const boxId = row.boxId || this.ids
      getVs(boxId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改VS";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.boxId != null) {
            updateVs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const boxIds = row.boxId || this.ids;
      this.$modal.confirm('是否确认删除VS编号为"' + boxIds + '"的数据项？').then(function () {
        return delVs(boxIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/vs/export', {
        ...this.queryParams
      }, `vs_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
